
# REQUIRES: Data/Roper Data.csv

# NOTE: Uses code from Stargazer Package Updates.R instead of the published version of Stargazer. See readme.txt.

################# Dependencies #################
# source("Load Packages.R")
# source("Analysis - Roper/Cleaning.R")

################# Iraq #################

roper_iraq <- roper[(roper$Campaign == "Iraq"),]

### Table 1b - Predictors of US Public Support for Withdrawal, 1946-2021
# With question features and percent of options that were for withdrawal
OLS_q <- lm(percwithdraw ~  bDeescalationOption + bOnesidedquestion + responseoptions,
            data = roper_iraq)

# With situation features
OLS_s <- lm(percwithdraw ~  logcas, 
            data = roper_iraq)

# With question and situation features
OLS_qs <- lm(percwithdraw ~  bDeescalationOption + bOnesidedquestion + responseoptions 
             + logcas, 
             data = roper_iraq)


# output <- stargazer(OLS_q, OLS_s, OLS_qs, title="Table 1b: Predictors of U.S. Public Support for Withdrawal, 1946-2021 - Iraq",
#                     align=TRUE, dep.var.labels=c(""), dep.var.caption="",
#                     column.labels=c(" ","",""),
#                     notes = "Results from OLS regressions <br> <span>&#42;</span>p<0.1;<span>&#42;&#42;</span>p<0.05;<span>&#42;&#42;&#42;</span>p<0.01", 
#                     notes.append = FALSE,
#                     covariate.labels=c("Troop Reduction Option", "One-sided Question",
#                                        "No. of Response Options", "Log Cumulative Casualties", "Constant"),
#                     omit.stat=c("LL","ser","f","adj.rsq"), no.space=TRUE, notes.align="c", out = "Plots/Table 1b - Iraq.html")

#webshot("Plots/Table 1b - Iraq.html", "Plots/Table 1b - Iraq.png", zoom=5, vwidth=420, vheight=400)
# Note: Add the independent variable groups in Word and remove "_R" from the file name
# webshot("Plots/Table 1b - Iraq.html", "Plots/Table 1b - Iraq.png", zoom=5, vwidth=475, vheight=580)

rm(OLS_q, OLS_qs, OLS_s, output)



################# Vietnam #################

roper_vietnam <- roper[(roper$Campaign == "Vietnam"),]

### Table 1b - Predictors of US Public Support for Withdrawal, 1946-2021
# With question features and percent of options that were for withdrawal
OLS_q <- lm(percwithdraw ~ bEnemyVictory + bOnesidedquestion + responseoptions,
            data = roper_vietnam)

# With situation features
OLS_s <- lm(percwithdraw ~  logcas, 
            data = roper_vietnam)

# With question and situation features
OLS_qs <- lm(percwithdraw ~  bEnemyVictory + bOnesidedquestion + responseoptions 
             + logcas, 
             data = roper_vietnam)


# output <- stargazer(OLS_q, OLS_s, OLS_qs, title="Table 1c: Predictors of U.S. Public Support for Withdrawal, 1946-2021 - Vietnam",
#                     align=TRUE, dep.var.labels=c(""), dep.var.caption="",
#                     column.labels=c(" ","",""),
#                     notes = "Results from OLS regressions <br> <span>&#42;</span>p<0.1;<span>&#42;&#42;</span>p<0.05;<span>&#42;&#42;&#42;</span>p<0.01", 
#                     notes.append = FALSE,
#                     covariate.labels=c("Enemy Victory Framing", "One-sided Question",
#                                        "No. of Response Options", "Log Cumulative Casualties", "Constant"),
#                     omit.stat=c("LL","ser","f","adj.rsq"), no.space=TRUE, notes.align="c", out = "Plots/Table 1c - Vietnam.html")
# 
# webshot("Plots/Table 1c - Vietnam.html", "Plots/Table 1c - Vietnam.png", zoom=5, vwidth=420, vheight=400)
# Note: Add the independent variable groups in Word and remove "_R" from the file name
# webshot("Plots/Table 1c - Vietnam.html", "Plots/Table 1c - Vietnam.png", zoom=5, vwidth=475, vheight=580)

rm(OLS_q, OLS_qs, OLS_s, output)



################# Iraq and Vietnam #################
OLS_qv <- lm(percwithdraw ~ bEnemyVictory + bOnesidedquestion + responseoptions,
             data = roper_vietnam)

OLS_qi <- lm(percwithdraw ~  bDeescalationOption + bOnesidedquestion + responseoptions,
            data = roper_iraq)

output <- stargazer(OLS_qv, OLS_qi, title="Table S4: The Effects of Each Frame in the Campaign in Which It Is Most Common – Enemy Victory in Vietnam and Troop Reduction in Iraq",
                    align=TRUE, dep.var.labels=c(""), dep.var.caption="",
                    column.labels=c(" ","",""),
                    digits = 3, digits.extra = 0,
                    notes = "P-value shown in parentheses below coefficient", 
                    notes.append = FALSE,
                    report = "vc*p",
                    covariate.labels=c("Enemy Victory Framing", "Troop Reduction Option", "One-sided Question",
                                       "No. of Response Options", "Constant"),
                    omit.stat=c("LL","ser","f","adj.rsq"), no.space=TRUE, notes.align="c", out = "Plots/Table A4 - Predictors of U.S. Public Support for Withdrawal, 1946-2021 - Vietnam and Iraq_R.html")

webshot("Plots/Table S4 - Predictors of U.S. Public Support for Withdrawal, 1946-2021 - Vietnam and Iraq_R.html", "Plots/Table S4 - Predictors of U.S. Public Support for Withdrawal, 1946-2021 - Vietnam and Iraq_R.png", zoom=5, vwidth=420, vheight=400)

# webshot("Plots/Table 1d - Vietnam and Iraq.html", "Plots/Table 1d - Vietnam and Iraq.png", zoom=5, vwidth=420, vheight=400)

rm(OLS_qi, OLS_qv, roper_iraq, roper_vietnam, output)
